using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace caseDrawer
{
	public abstract class AbstractGenerator : IGenerator
	{
		IWriter writer;
		public IWriter Writer{
			get{return writer;}			
		}
		
		DataGridViewRowCollection rows;
		public DataGridViewRowCollection Rows{
			get{return rows;}
			set{rows = value;}
		}
		
		string testCaseFolder;
		public string TestCaseFolder{
			get{return testCaseFolder;}
			set{testCaseFolder = value;}
		}
		
		Senario senario;
		public Senario Senario{
			get{return senario;}
			set{senario = value;}
		}
		
		IList<string> results = new List<string>();
		
		
		public AbstractGenerator()
		{
			this.writer = WriterFactory.Instance.getWriter("TXTWRITER");			
		}
		
		public abstract void generateTestCase();
		
		protected string generateTitle_Psv(Senario s){
			StringBuilder title = new StringBuilder();
			title.Append("verify ");
			title.Append(s.Name);
			title.Append(" ");
			title.Append(s.Comment);
			title.Append("\n");
			return title.ToString();
		}
		
		protected string generateTitle_Ngv(Senario s){
			StringBuilder title = new StringBuilder();
			title.Append("verify ");
			title.Append(s.Name);
			title.Append(" ");
			title.Append(s.Comment);
			return title.ToString();
		}
		
		protected IList<string> generateResults_Psv(DataGridViewRowCollection Rows,char grp){

			IList<string> results = new List<string>();
			
			results.Add("[STEP]");
			
			int stepnum = 1;
			foreach(DataGridViewRow row in Rows){
				if(row.IsNewRow)
					break;
				
				string id			= row.Cells["cid"].Value.ToString();
				string eventStr 	= convertToAction( row.Cells["caccessEvents"].Value.ToString() );
				string name			= row.Cells["cdescription"].Value.ToString();
								
				DataTable tbBrickResult = Database.Instance.loadResults(id,grp);
				
				string input="";
				string output="";
				if(tbBrickResult.Rows.Count != 0){
					input  = tbBrickResult.Rows[0]["input"].ToString();
					output = tbBrickResult.Rows[0]["output"].ToString();
				}
				results.Add(writeSteps_Psv(stepnum, id,eventStr,name,input,output));

				stepnum++;
			}

			return results;
		}

		string writeSteps_Psv(int stepnum, string brickid,string eventStr,string name,string input,string output){
			StringBuilder step = new StringBuilder();
			
			step.Append(stepnum);
			step.Append(": ");

			step.Append("User ");
			step.Append(eventStr);
			
			if(eventStr=="input "){
				if(input == "")
					step.Append(" { VALUE } ");
				else
					step.Append("{"+ input +"}");
				
				step.Append(" at ");
			}

			step.Append(name);

			step.Append("		-> At [STEP] ");
			step.Append(stepnum);
			step.Append(": ");
			step.Append(output);

			return step.ToString();
		}
		
		
		protected string convertToAction(string str){
			string tmp = "";
			switch (str) {
				case "1":
					tmp = "hover mouse over ";
					break;
				case "2":
					tmp ="press mouse left key down on ";
					break;
				case "3":
					tmp ="scroll mouse on ";
					break;
				case "4":
					tmp ="move mouse away from ";
					break;
				case "5":
					tmp ="input ";
					break;
				case "6":
					tmp ="click ";
					break;
				case "7":
					tmp ="double click ";
					break;
				case "8":
					tmp ="right click ";
					break;					
				case "9":
					tmp ="double right click ";
					break;				
			}
			
			return tmp;
		}
	
	}
}
